﻿Imports System.Data.OleDb
Public Class frmNhom

    Private Sub frmNhom_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.BackColor = Color.FromArgb(194, 217, 247)
            Nhom_Load()
        Catch ex As Exception

        End Try
    End Sub
#Region "Load Data"
    Protected Sub Nhom_Load()
        Try
            Dim dtNhom As New DataTable
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim adapter As OleDb.OleDbDataAdapter
            Dim dbCommand As OleDb.OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            dbCommand = New OleDbCommand("spNhom_GetAll", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            adapter = New OleDb.OleDbDataAdapter(dbCommand)
            adapter.Fill(dtNhom)
            dbConn.Close()
            dgvNhom.AutoGenerateColumns = False
            dgvNhom.DataSource = dtNhom
        Catch ex As Exception

        End Try
    End Sub
#End Region

    Private Sub cmdAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddNew.Click
        Try
            ShowModalForm(frmThemNhom, False, False)
            frmThemNhom.NhomId = -1
            Nhom_Load()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        Try
            Dim NhomId As Integer = dgvNhom("NhomId", dgvNhom.CurrentRow.Index).Value
            frmThemNhom.NhomId = NhomId
            ShowModalForm(frmThemNhom, False, False)
            frmThemNhom.NhomId = -1
            Nhom_Load()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Try

            Dim dbConn As New OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dbCommand As OleDbCommand
            Dim NhomId As Integer = dgvNhom("NhomId", dgvNhom.CurrentRow.Index).Value
            Dim TenNhom As String = dgvNhom("TenNhom", dgvNhom.CurrentRow.Index).Value
            Dim dlgRes As DialogResult
            dlgRes = MessageBox.Show( _
                  "Bạn muốn xóa thông tin nhóm : " & TenNhom, _
                  "Xóa nhóm sản phẩm", _
            MessageBoxButtons.OKCancel, _
                  MessageBoxIcon.Question)
            If dlgRes = DialogResult.OK Then
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                dbCommand = New OleDbCommand("spNhom_Delete", dbConn)
                dbCommand.CommandType = CommandType.StoredProcedure
                dbCommand.Parameters.Add("@NhomId", OleDbType.Integer).Value = NhomId
                dbCommand.Parameters.Add("@NguoiXoa", OleDbType.VarWChar).Value = NguoiTao()
                dbCommand.Parameters.Add("@NgayXoa", OleDbType.VarWChar).Value = NgayTao()
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
                Nhom_Load()
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class